package com.wt.struggle.goodbye.main.fragment

import android.content.Intent
import android.os.Bundle
import android.os.Message
import android.support.v7.widget.StaggeredGridLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.google.gson.reflect.TypeToken
import com.wt.struggle.goodbye.R
import com.wt.struggle.goodbye.base.Config
import com.wt.struggle.goodbye.base.ItemClickListener
import com.wt.struggle.goodbye.base.ProV4Fragment
import com.wt.struggle.goodbye.base.Send
import com.wt.struggle.goodbye.info.NetInfo
import com.wt.struggle.goodbye.info.NewsInfo
import com.wt.struggle.goodbye.main.activity.InvitationDetailsActivity
import com.wt.struggle.goodbye.main.adapter.TuiShopAdapter
import kotlinx.android.synthetic.main.search_list_layout.*

/**
 * 搜索到的数据列表
 */
class SearchListFragment : ProV4Fragment() {

    override fun handler(what: Int, arg1: Int, netInfo: NetInfo) {
        when (what) {

            Send.SEARCH_LIST -> {

                removeLoadDialog()

                if (netInfo.code == Config.SUCCESS) {

                    val result = netInfo.dataResult
                    if (result != null && result != "" && result != "null") {
                        val arr: ArrayList<NewsInfo> =
                            gson!!.fromJson(result, object : TypeToken<ArrayList<NewsInfo>>() {}.type)

                        if (arr.size != 0) {
                            adapter.updateDataClear(arr)
                        } else {
                            showToastShort(activity!!, "暂无数据")
                        }

                    } else {
                        showToastShort(activity!!, "暂无数据")
                    }
                }
            }
        }
    }

    var resultStr: String = ""

    override fun createView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        return inflater.inflate(R.layout.search_list_layout, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        initAdapter()

        searchEditText.setOnEditorActionListener { textView, p, event ->

            send!!.getSearchList(1, 10, 0, 0, 0, searchEditText.text.toString(), null, handler!!)
            showLoadDialog(activity!!)

            false
        }

        tvCancel.setOnClickListener {

            val content = searchEditText.text.toString()
            send!!.getSearchList(1, 10, 0, 0, 0, content, null, handler!!)
            showLoadDialog(activity!!)

        }

        if (resultStr != "") {
            showList()
        }

        imageBack.setOnClickListener {
            activity!!.finish()
        }


    }

    private fun showList() {
        val arr: ArrayList<NewsInfo> = gson!!.fromJson(resultStr, object : TypeToken<ArrayList<NewsInfo>>() {}.type)
        adapter.updateData(arr)
    }


    lateinit var adapter: TuiShopAdapter
    val list = arrayListOf<NewsInfo>()

    private fun initAdapter() {
        recyclerView.isNestedScrollingEnabled = false
        recyclerView.layoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
        adapter = TuiShopAdapter(activity!!, list)
        recyclerView.adapter = adapter
        adapter.itemClickListener = object : ItemClickListener {

            override fun onItemClick(position: Int) {
                val intent = Intent(activity!!, InvitationDetailsActivity::class.java)
                val info = list[position]
                intent.putExtra("info", info)
                startActivity(intent)

            }

            override fun onLongClick(position: Int) {

            }

        }
    }

}